package org.come.Jpanel;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JList;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.ScrollPaneConstants;

import org.come.bean.AllAchieve;
import org.come.model.Achieve;
import org.come.until.ScrollUI;
import org.come.until.SrcollPanelUI;
import org.come.until.UserMessUntil;

import com.tool.ModerateTask.TaskRoleData;
import com.tool.btn.FormsOnOffBtn;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;



/**
 * @author HGC<br>
 * @time 2020年3月20日 下午2:43:37<br>
 * @class 类名:PartnerArenaExchangePanel 伙伴竞技兑换面板 编号:107<br>
 */
public class PartnerArenaExchangePanel extends JPanel {

    private JScrollPane scrollPane;
    private JList<PartnerArenaExchangeModelPanel> partnerUnitJpanels;

    public PartnerArenaExchangePanel() {
    		setPreferredSize(new Dimension(522, 450));
            setOpaque(false);
            setLayout(null);

            // 关闭按钮事件
            FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 107);
            offBtn.setBounds(522 - 37, 10, 25, 25);
            this.add(offBtn);

            getScrollPane();
            showView();
    }

    private int typeView = -1;
    /** 展示面板 */
    public void showView() {
        AllAchieve allAchieve = UserMessUntil.getAllAchieve();
        if(allAchieve == null){
            return;
        }
        //获取胜利场次
        int sumReceive = TaskRoleData.SumReceive(4, 2);
        if(typeView == -1){
            typeView = 1;
            List<Achieve> achieves = allAchieve.getAchieves();
            int size = achieves.size();
            for (int i = 0; i < size; i++) {
                PartnerArenaExchangeModelPanel modelPanel = new PartnerArenaExchangeModelPanel();
                modelPanel.setBounds(10 + (i % 2) * 213, 0 + (i / 2) * 75, 203, 68);
                modelPanel.showView(achieves.get(i), sumReceive);
                partnerUnitJpanels.add(modelPanel);
            }
            int num = size % 2 == 0 ? size / 2: size / 2+1 ;
            partnerUnitJpanels.setPreferredSize(new Dimension(439, num * 75));
        }else{
            int componentCount = partnerUnitJpanels.getComponentCount();
            for (int i = 0; i < componentCount; i++) {
                PartnerArenaExchangeModelPanel component2 = (PartnerArenaExchangeModelPanel) partnerUnitJpanels.getComponent(i);
                component2.refreshView(sumReceive);
            }
        }
    }

    private ImageIcon icon;

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
            if (icon == null) {
                icon = new ImageIcon("inkImg/newbackground/B311.png");
            }
            g.drawImage(icon.getImage(), 0, 0, 522, 450, null);
    }

    public JScrollPane getScrollPane() {
    		if (scrollPane == null) {
                // 属性滚动条
                scrollPane = new JScrollPane();
                scrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
                scrollPane.getViewport().setView(getPartnerUnitJpanels());
                scrollPane.getVerticalScrollBar().setUI(new SrcollPanelUI());
                scrollPane.getVerticalScrollBar().setUnitIncrement(20);
                scrollPane.getViewport().setOpaque(false);
                scrollPane.setOpaque(false);
                scrollPane.setBounds(47, 52, 451 + 3, 375);
                scrollPane.setBorder(BorderFactory.createEmptyBorder());
                scrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
                this.add(scrollPane);
            }
        return scrollPane;
    }

    public void setScrollPane(JScrollPane scrollPane) {
        this.scrollPane = scrollPane;
    }

    public JList<PartnerArenaExchangeModelPanel> getPartnerUnitJpanels() {
        if (partnerUnitJpanels == null) {
            partnerUnitJpanels = new JList<PartnerArenaExchangeModelPanel>();
            partnerUnitJpanels.setSelectionBackground(new Color(122, 117, 112));
            partnerUnitJpanels.setSelectionForeground(Color.white);
            partnerUnitJpanels.setForeground(Color.white);
            partnerUnitJpanels.setFont(UIUtils.TEXT_HY16);
            partnerUnitJpanels.removeAll();
            partnerUnitJpanels.setBackground(UIUtils.Color_BACK); // 设置列表框为透明背景
            // DefaultListCellRenderer cellRenderer = new
            // DefaultListCellRenderer() {
            // @Override
            // public Component
            // getListCellRendererComponent(javax.swing.JList<?> list, Object
            // value, int index,
            // boolean isSelected, boolean cellHasFocus) {
            // super.getListCellRendererComponent(list, value, index,
            // isSelected, cellHasFocus);
            // return this;
            // }
            // };
            // partnerUnitJpanels.setCellRenderer(cellRenderer);
            partnerUnitJpanels.setOpaque(false);
        }
        return partnerUnitJpanels;
    }

    public void setPartnerUnitJpanels(JList<PartnerArenaExchangeModelPanel> partnerUnitJpanels) {
        this.partnerUnitJpanels = partnerUnitJpanels;
    }
}
